package org.dromara.teacher.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.teacher.domain.vo.XjTeacherTaskVo;
import org.dromara.teacher.domain.bo.XjTeacherTaskBo;
import org.dromara.teacher.service.IXjTeacherTaskService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 教师作业
 *
 * @author xjb
 * @date 2024-12-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/teacher/task")
public class XjTeacherTaskController extends BaseController {

    private final IXjTeacherTaskService xjTeacherTaskService;

    /**
     * 查询教师作业列表
     */
    @SaCheckPermission("teacher:task:list")
    @GetMapping("/list")
    public TableDataInfo<XjTeacherTaskVo> list(XjTeacherTaskBo bo, PageQuery pageQuery) {
        return xjTeacherTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出教师作业列表
     */
    @SaCheckPermission("teacher:task:export")
    @Log(title = "教师作业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(XjTeacherTaskBo bo, HttpServletResponse response) {
        List<XjTeacherTaskVo> list = xjTeacherTaskService.queryList(bo);
        ExcelUtil.exportExcel(list, "教师作业", XjTeacherTaskVo.class, response);
    }

    /**
     * 获取教师作业详细信息
     *
     * @param taskId 主键
     */
    @SaCheckPermission("teacher:task:query")
    @GetMapping("/{taskId}")
    public R<XjTeacherTaskVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String taskId) {
        return R.ok(xjTeacherTaskService.queryById(taskId));
    }

    /**
     * 新增教师作业
     */
    @SaCheckPermission("teacher:task:add")
    @Log(title = "教师作业", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody XjTeacherTaskBo bo) {
        return toAjax(xjTeacherTaskService.insertByBo(bo));
    }

    /**
     * 修改教师作业
     */
    @SaCheckPermission("teacher:task:edit")
    @Log(title = "教师作业", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody XjTeacherTaskBo bo) {
        return toAjax(xjTeacherTaskService.updateByBo(bo));
    }

    /**
     * 删除教师作业
     *
     * @param taskIds 主键串
     */
    @SaCheckPermission("teacher:task:remove")
    @Log(title = "教师作业", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] taskIds) {
        return toAjax(xjTeacherTaskService.deleteWithValidByIds(List.of(taskIds), true));
    }
}
